package service;

import mapper.MasterMapper;
import mapper.UserMapper;
import model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import util.Page;

import java.util.HashMap;
import java.util.List;


@Service

@Transactional
public class MasterService {


    @Autowired

    private MasterMapper masterMapper;


    public Master check_master_login(String username, String password){

        return masterMapper.check_master_login(username,password);
    }


    public Page<User> findByPage(int currentPage, HashMap<String, Object> params) {

        HashMap<String, Object> map = new HashMap<String, Object>();

        Page<User> page = new Page<User>();

        //封装当前页数

        page.setCurrPage(currentPage);

        //每页显示的数据

        int pageSize = 4;//当前总共是9条，那么可以计算得到3页数据

        page.setPageSize(pageSize);

        //封装带条件查询的总记录数

        int totalCount = masterMapper.selectCount(params);

        page.setTotalCount(totalCount);

        //封装总页数

        Integer totalcount = totalCount;

        //计算页数，如果整除，那么直接返回冒号左边的值，否则还需要加1

        Integer num = totalcount%pageSize==0? totalcount / pageSize: totalcount / pageSize+1;

        page.setTotalPage(num.intValue());//转换为整数

        map.put("start", (currentPage - 1) * pageSize);//起始的记录值

        map.put("size", page.getPageSize());//获取每个页面的pageSize

        // map.put("code",params.get("code"));

//        map.put("code",params.get("code"));

        map.put("username",params.get("username"));
//        map.put("age",params.get("age"));
        map.put("gender",params.get("gender"));
//        map.put("password",params.get("password"));
        // map.put("gender",params.get("gender"));



        //  map.put("查询条件参数",查询值)   如果后面要做带查询条件的分页，可以在这里添加查询参数，而查询条件参数可以在当前findByPage的方法里加一个参数

        List<User> list = masterMapper.findByPage(map);

        page.setList(list);

        return page;

    }

    public Page<game_list> findByPage_apply(int currentPage, HashMap<String, Object> params) {

        HashMap<String, Object> map = new HashMap<String, Object>();

        Page<game_list> page = new Page<game_list>();

        //封装当前页数

        page.setCurrPage(currentPage);

        //每页显示的数据

        int pageSize = 4;//当前总共是9条，那么可以计算得到3页数据

        page.setPageSize(pageSize);

        //封装带条件查询的总记录数

        int totalCount = masterMapper.selectCount_apply(params);

        page.setTotalCount(totalCount);

        //封装总页数

        Integer totalcount = totalCount;

        //计算页数，如果整除，那么直接返回冒号左边的值，否则还需要加1

        Integer num = totalcount%pageSize==0? totalcount / pageSize: totalcount / pageSize+1;

        page.setTotalPage(num.intValue());//转换为整数

        map.put("start", (currentPage - 1) * pageSize);//起始的记录值

        map.put("size", page.getPageSize());//获取每个页面的pageSize

        // map.put("code",params.get("code"));

//        map.put("code",params.get("code"));
       // map.put("id",params.get("id"));
        map.put("game_type",params.get("game_type"));
//        map.put("age",params.get("age"));
        map.put("game_name",params.get("game_name"));
        map.put("player",params.get("player"));
//        map.put("password",params.get("password"));
        // map.put("gender",params.get("gender"));



        //  map.put("查询条件参数",查询值)   如果后面要做带查询条件的分页，可以在这里添加查询参数，而查询条件参数可以在当前findByPage的方法里加一个参数

        List<game_list> list = masterMapper.findByPage_apply(map);

        page.setList(list);

        return page;

    }



    public List<game_list>get_game_list_m(){


        return this.masterMapper.get_game_list_m();


    }




    public game_list find_game_list_ById(int id) {

        return this.masterMapper.find_game_list_ById(id);

    }




    public void update_play_state(game_list game_list){

        this.masterMapper.update_play_state(game_list);

    }


    public void update_game_list(game_list game_list){

        this.masterMapper.update_game_list(game_list);

    }


    public void delete(int id){

        this.masterMapper.delete(id);

    }


}
